package com.pxst.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
	@Autowired
	private RedisConnectionFactory redisConnectionFactory;

	@Bean
	public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
	    //Redis消息监听器
	    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
	    //设置Redis链接工厂
	    container.setConnectionFactory(connectionFactory);
	    return container;
	}
	@Bean
	public RedisTemplate<String, Object> functionDomainRedisTemplate() {
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate();
		initDomainRedisTemplate(redisTemplate, this.redisConnectionFactory);
		return redisTemplate;
	}

	private void initDomainRedisTemplate(RedisTemplate<String, Object> redisTemplate, RedisConnectionFactory factory) {
		redisTemplate.setKeySerializer((RedisSerializer) new StringRedisSerializer());
		redisTemplate.setHashKeySerializer((RedisSerializer) new StringRedisSerializer());
		redisTemplate.setHashValueSerializer((RedisSerializer) new JdkSerializationRedisSerializer());
		redisTemplate.setValueSerializer((RedisSerializer) new JdkSerializationRedisSerializer());
		redisTemplate.setKeySerializer((RedisSerializer) new StringRedisSerializer());
		redisTemplate.setValueSerializer((RedisSerializer) new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashKeySerializer((RedisSerializer) new GenericJackson2JsonRedisSerializer());
		redisTemplate.setHashValueSerializer((RedisSerializer) new GenericJackson2JsonRedisSerializer());
		redisTemplate.setConnectionFactory(factory);
	}

	@Bean
	public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForHash();
	}

	@Bean
	public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForValue();
	}

	@Bean
	public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForList();
	}

	@Bean
	public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForSet();
	}

	@Bean
	public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) {
		return redisTemplate.opsForZSet();
	}
}
